<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Python development in Netbeans, Early access</title>
<link rel="stylesheet" href="../format.css" type="text/css">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="keywords" content="Python, Netbeans, IDE, Early Access, development">
<meta name="description" content="Python development in Netbeans, Early access">
<meta name="language" content="en">
<meta name="author" content="Jan Bodnar">
<meta name="distribution" content="global">

<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-5536206-1']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>

</head>


<body>

<div class="ltow">
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* NewVertical */
google_ad_slot = "4231459001";
google_ad_width = 120;
google_ad_height = 600;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>

<div class="content">

<a href="/" title="Home">Home</a>


<h1 class="header">Python development in Netbeans</h1>


<h2>NetBeans</h2>

<p>
NetBeans is a world class platform for creating applications. Originally, it was a Java IDE. 
With each release, NetBeans is becomming more versatile. The newiest NetBeans 6.5 release has a new module 
for creating PHP applications. Ruby and C/C++ development modules were added in the past.  
</p>

<div class="center">
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* horizontal */
google_ad_slot = "1734478269";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>

<p>
In this article, we will talk about a new module for the Python programming language. 
</p>


<h2>Python EA IDE</h2>

<p>
NetBeans has recently released an Early Access IDE for the Python programming language. Fully functional Python IDE 
has been an old request or dream of many Python programmers. This NetBeans subproject seems very promising. 
Currently the project is under development. It has been released as a early access project. 
It can be downloaded from the following <a href="http://www.netbeans.org/features/python/index.html">link</a>.
</p>

<p>
NetBeans community already created an IDE for PHP and Ruby programming languages. Python is the next logical step. 
This way, the NetBeans platform brings another very popular language to it's portfolio. 
</p>


<p>
Netbeans itself is created in Java language and Swing GUI. This combination is ideal for a multiplatform, multilanguage IDE.
</p>

<h2>First impressions</h2>

<p>
Installing of NetBeans for Python was very easy. The GTK theme was greatly improved and it looks very well. 
(This comes from a recent JDK version, not NetBeans itself.)
The application is very responsive and fast. On my notebook with 512 MB RAM and 1.6 GB processor, NetBeans 
performed very well. Without specialized benchmarking tools, there is no way how you can tell it from a C/C++ 
application. Except perhaps, when you run out of memory and do a lot of swapping. 
</p>

<p>
System requirements for this Python EA module were not specified on the official NetBeans site. 
These are typical general system requirements for Ubuntu Linux: 
Minimal system requirements are: 800 MHz processor, 512 MB memory, 650 MB free disk space. Recommended
system requirements are: 2.6 GHz processor, 1 GB memory and 850 free disk space.
</p>


<h2>The Look and Feel</h2>

<p>
On Linux, the default is the GTK theme.  Developers can choose among three lafs. We have the GTK theme, the metal theme and the motif theme.  
The motif laf looks interesting, but it has several serious design bugs. The GTK theme has <i>vastly</i> improved in the recent versions of JDK.
</p>

<br>
<img src="../images/netbeanspython/metallaf.png" alt="Metal look and feel">
<div class="figure">Figure: Metal look and feel</div>
<br>

<br>
<img src="../images/netbeanspython/gtklaf.png" alt="GTK look and feel">
<div class="figure">Figure: GTK look and feel</div>
<br>


<h2>The IDE</h2>

<p>
The following screenshot shows the NetBeans IDE with the traditional metal look and feel.
</p>

<br>
<img src="../images/netbeanspython/netbeans.png" alt="NetBeans IDE">
<div class="figure">Figure: NetBeans IDE</div>
<br>

<p>
The NetBeans IDE is divided into several parts, called simply windows. These are probably the most common windows:
</p>

<ol>
<li>Projects window</li>
<li>Source editor window</li>
<li>Output window</li>
</ol>


<p>
In the projects window, we create and manage our software projects. In the source editor window, we edit our source, metafile, makefile and other editable files. The NetBeans IDE uses the output window for showing various messages. Typically build and run messages. 
</p>

<h2>Features</h2>

<ul>
<li>Code folding</li>
<li>Code completion</li>
<li>Syntax highlighting</li>
<li>Database support</li>
<li>Web services support</li>
<li>Dynamic variable highlighting</li>
<li>Multiline code commenting</li>
</ul>


<p>
The following screenshot shows a NetBeans code editor. We can see that comments, keywords and text are all 
shown in different color. The plus sign also shows the code folding feature. 
</p>

<br>
<img src="../images/netbeanspython/codeeditor.png" alt="Code editor">
<div class="figure">Figure: Code editor</div>
<br>


<p>
The next screenshot shows a dynamic variable highlighting in action. This is a very handy feature. If you place a cursor on a variable, all
occurences of this variable are marked. The background of the text is highlighted in a different color. It looks
good and is unobtrusive. Unlike eg. in Eclipse. This feature is not available in JDeveloper. 
</p>

<br>
<img src="../images/netbeanspython/letters.png" alt="Variable highlighting">
<div class="figure">Figure: Variable highlighting</div>
<br>



<h2>Creating a project</h2>

<p>
First, we need to create a Python project. There are several ways, how we can create a Python project. From File menu, we select New Project option, or use the Ctrl+Shift+N shortcut. We can also create the project by using the context menu, right click on the project window and 
select New Project from the pop up window. The fastest way is to click on the new project button on the edit toolbar. 
</p>

<br>
<img src="../images/edittoolbar.png" alt="The edit toolbar">
<div class="figure">Figure: The edit toolbar</div>
<br>


<p>
Next we do two steps. Choose a type of a project and then provide a name and location for the project. 
From the Categories box, we select Python, from the Projects box, Python Project.
</p>


<p>
In the next dialog window, we provide a name and location for our Python Project. There is also a check box. It
is checked by default. It means, that by default a new project is set to be the main project. We can have several 
projects opened in Netbeans. If we click on the build, debug or run button, this is applied to the current main project
in the IDE. We can set, unset a main project from the File menu, or by right clicking on the project icon in the projects
window. 
</p>

<p>
The last important thing is to choose the correct version of Python interpreter. There are two options. The classic CPython and Jython.
The Jython comes prebuilt with Netbeans. The CPython was located during the installation proces. In our article, we will work with CPython 
examples.  
</p>

<br>
<img src="../images/netbeanspython/pythonproject.png" alt="Python Project">
<div class="figure">Figure: Python Project</div>
<br>

<br>
<hr style="width:60%">
<br>

<h2>Console Python example</h2>

<p>
In the first example, we create a simple Python console script. 
</p>


<pre class="code">
#!/usr/bin/python

# console.py


print "Netbeans IDE 6.5"
print "for Python language"
print "Early access"
</pre>

<p>
We create a new project as shown previously and type in the code. Execute the script by pressing Shift + F6.
</p>

<p>
We should see a text printed in the console window. 
</p>

<br>
<img src="../images/netbeanspython/console.png" alt="Console">
<div class="figure">Figure: Console</div>
<br>


<h2>Simple PyGTK application</h2>

<p>
In the next example, we will create a PyGTK application.
</p>

<p>
Creating a PyGTK application worked out of the box on Linux. No additional settings were necessary. We just need to type in the code and 
launch the application with the Shift + F6 key combination.
</p>

<p>
Same for wxPython or PyQt4 applications. 
</p>

<pre class="code">
#!/usr/bin/python

# simple.py

import pygtk
pygtk.require('2.0')
import gtk

class Simple:

    def destroy(self, widget, data=None):
        gtk.main_quit()

    def __init__(self):
        win = gtk.Window(gtk.WINDOW_TOPLEVEL)

        win.connect("destroy", self.destroy)

        win.set_title("Simple")
        win.set_position(gtk.WIN_POS_CENTER)
        win.show_all()

Simple()
gtk.main()
</pre>


<p>
This small code example creates a window in PyGTK library. After pressing Shift + F6 a small window will pop up.
(The window might be minimized by default).
</p>

<br>
<img src="../images/netbeanspython/simple.png" alt="Simple PyGTK application">
<div class="figure">Figure: Simple PyGTK application</div>
<br>


<h2>MySQL database example</h2>

<p>
NetBeans Python IDE EA comes with a built-in database support. We can create databases, tables or execute SQL commands inside
the IDE environment. 
</p>

<p>
In our example, we will create a friends database, friends table and insert some data into the table. A Python script will fetch the
data from the table and print them into the console window. We will use MySQL database. 
</p>

<p>
For this example to work, you should have MySQL database system running and have <b class="keyword">MySQLdb</b> module installed.
</p>

<p>
To check, if you have MySQL RDMS running, execute the following command:
</p>

<pre class="code">
$ ps -ef | grep mysql
root      5391     1  0 15:35 ?        00:00:00 /bin/sh /usr/bin/mysqld_safe
...
</pre>

<p>
The following commands search for and install a Python MySQL database module
</p>

<pre class="code">
$ apt-cache search mysqldb
python-mysqldb - A Python interface to MySQL
python-mysqldb-dbg - A Python interface to MySQL (debug extension)
eikazo - graphical frontend for SANE designed for mass-scanning
sql-editor - editor of SQL databases, with 'join' capability

$ sudo apt-get install python-mysqldb
</pre>


<p>
In the Services window, you can see a database and a web services icon. The services window is whown with 
Ctrl + 5. The MySQL RDMS was running, when I launched the IDE, so NetBeans recongized it. 
Otherwise we have to create a new connection by right clicking on the icon. 
</p>


<br>
<img src="../images/netbeanspython/services.png" alt="Services">
<div class="figure">Figure: Services</div>
<br>

<p>
The first node in the Databases tree shows a MySQL server instance. The instance has 5 different databases. 
We create a friends database. Right click on the server instance icon and select Create Database... option.
In the dialog window, we specify a name for our database, e.g. friends. 
</p>

<p>
Next we right click on the database friends, and select Connect... option. We specify user name and password.
Upon successful connection, we shoud see complete connection icon for the friends database.
</p>


<br>
<img src="../images/netbeanspython/connection.png" alt="Database Connection">
<div class="figure">Figure: Database Connection</div>
<br>

<p>
We then right click on the database connection icon and select Execute Command... option. 
A SQL Commnad window appears, where we will write out SQL commnads. 
</p>



<pre class="code">
CREATE TABLE friends
(
  id int not null auto_increment primary key,
  name varchar(40),
  age int
);
</pre>


<p>
We create a friends table. To execute this SQL command, click on the Run SQL icon, or press Ctrl + Shift + E.
</p>


<pre class="code">
INSERT INTO friends (name, age) VALUES
  ("Erika", 21 ),
  ("Robert", 34),
  ("Mike", 22),
  ("Roman", 29),
  ("Lenka", 18)
</pre>


<p>
Here we populate the table with some data. 
</p>


<p>
When we run <code>SELECT * FROM friends</code> SQL code, we get a new window, where we can 
insert new record, update or delete records. 
</p>

<br>
<img src="../images/netbeanspython/friends.png" alt="friends table">
<div class="figure">Figure: friends table</div>
<br>

<p>
Finally, we write the Python script.
</p>

<pre class="code">
#!/usr/bin/python

import MySQLdb

try:
    conn = MySQLdb.connect(host="localhost", user="root",
        passwd="", db="friends")
    cursor = conn.cursor()
    cursor.execute("select * from friends")

except MySQLdb.Error, e:
     print "Error %d: %s" % (e.args[0], e.args[1])
     sys.exit(1)

else:
    data = cursor.fetchall()
    for id, name, age in data:
        print name, age

    cursor.close()
    conn.close()
</pre>


<p>
If all went OK, we get the following list of friends in the output window. 
</p>


<pre class="code">
Erika 21
Robert 34
Mike 22
Roman 29
Lenka 18
</pre>


<h2>Issues</h2>

<p>
I have encountered several issues when working with Python IDE. The IDE is in beta state and it is not finished yet. 
I will mention some of them. 
</p>

<p>
For example, there are issues with the code folding. Code completion does not work well. Too many options pop up, so the 
programmer is lost in all those values. No matter what I do, I can't get rid of the start page. It is still there, when you
restart NetBeans. When you resize the window to a small size and bring it back to original size, toolbars are scattered.
The about dialog is huge, but says nothing about the fact, that it is a Python IDE. 
The Ctrl + / keys comment the code, but this key combination is not available in any of the menus. 
etc.
</p>


<h2>Final word</h2>

<p>
The NetBeans IDE for the Python language looks promising. There are plans for Django and TurgoGear web frameworks.
Python programmers can look forward to a finally good IDE for their programming language. 
</p>



<div class="center">
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* horizontal */
google_ad_slot = "1734478269";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>
<br>


<div class="botNav, center">
<span class="botNavItem"><a href="/">Home</a></span> ‡ <span class="botNavItem"><a href="#">Top of Page</a></span>
</div>


<div class="footer">
<div class="signature">
<a href="/">ZetCode</a> created November 30, 2008  <span class="copyright">&copy; 2007 - 2012 Jan Bodnar</span>
</div>
</div>

</div> <!-- content -->


</body>
</html>

